package com.jz.digital.view

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.jz.digital.R
import kotlinx.android.synthetic.main.item_common_material_requisition_layout.view.img_right
import kotlinx.android.synthetic.main.item_common_material_requisition_layout.view.line
import kotlinx.android.synthetic.main.item_common_material_requisition_layout.view.tv_content
import kotlinx.android.synthetic.main.item_common_material_requisition_layout.view.tv_edit
import kotlinx.android.synthetic.main.item_common_material_requisition_layout.view.tv_label

/**
 * @author jiangdz
 * @date 2024/11/19
 * @time 19:30
 * 用途:
 */
class CommonMaterialLabel(context: Context, attributeSet: AttributeSet?) :
    LinearLayout(context, attributeSet) {
    private var mShowLabel = true
    private var mLabelText: String = ""
    private var mLabelColor: Int = Color.BLACK
    private var mLabelSize = 0
    private var mShowContent = true
    private var mContentText: String = ""
    private var mContentColor: Int = Color.BLACK
    private var mContentSize = 0
    private var mShowEdit = false
    private var mEditText: String = ""
    private var mEditHideText: String = ""
    private var mEditColor: Int = Color.BLACK
    private var mEditSize = 0
    private var mRightImgResId: Int = View.NO_ID
    private var mRightImgColor = View.NO_ID
    private var mShowRightImg = false
    private var mLineColor: Int = View.NO_ID
    private var mShowLine = true

    init {
        orientation = HORIZONTAL
        gravity=Gravity.CLIP_VERTICAL
        inflate(context, R.layout.item_common_material_requisition_layout, this)
        val typeArray =
            context.obtainStyledAttributes(attributeSet, R.styleable.CommonMaterialLabel)
        with(typeArray) {
            mLabelText = getString(R.styleable.CommonMaterialLabel_material_label) ?: ""
            mLabelColor =
                getColor(R.styleable.CommonMaterialLabel_material_label_color, resources.getColor(R.color.color_131825))
            mLabelSize = getDimensionPixelSize(
                R.styleable.CommonMaterialLabel_material_label_size,
                resources.getDimensionPixelSize(R.dimen.size12sp)
            )
            mContentText = getString(R.styleable.CommonMaterialLabel_material_content) ?: ""
            mContentColor =
                getColor(R.styleable.CommonMaterialLabel_material_content_color, resources.getColor(R.color.color_82868F))
            mContentSize = getDimensionPixelSize(
                R.styleable.CommonMaterialLabel_material_content_size,
                resources.getDimensionPixelSize(R.dimen.size12sp)
            )
            mRightImgResId =
                getResourceId(R.styleable.CommonMaterialLabel_material_right_image, mRightImgResId)
            mRightImgColor =
                getColor(R.styleable.CommonMaterialLabel_material_right_image_color, mRightImgColor)
            mShowLabel = getBoolean(R.styleable.CommonMaterialLabel_material_label_show, mShowLabel)
            mShowContent =
                getBoolean(R.styleable.CommonMaterialLabel_material_content_show, mShowContent)
            mShowRightImg =
                getBoolean(R.styleable.CommonMaterialLabel_material_right_image_show, mShowRightImg)
            mShowLine = getBoolean(R.styleable.CommonMaterialLabel_material_line_show, mShowLine)
            mLineColor = getColor(R.styleable.CommonMaterialLabel_material_line_color, mLineColor)
            mShowEdit=getBoolean(R.styleable.CommonMaterialLabel_material_edit_show,mShowEdit)
            mEditText=getString(R.styleable.CommonMaterialLabel_material_edit)?:""
            mEditHideText = getString(R.styleable.CommonMaterialLabel_material_edit_hide) ?: ""
            mEditColor=getColor(R.styleable.CommonMaterialLabel_material_edit_color,mEditColor)
            mEditSize=getDimensionPixelSize(R.styleable.CommonMaterialLabel_material_edit_size,mEditSize)
        }
        typeArray.recycle()
        initView()
    }

    private fun initView() {
        if (mShowLabel) {
            getLabelWidget().visibility = View.VISIBLE
            getLabelWidget().text = mLabelText
            if (mLabelColor != View.NO_ID) {
                getLabelWidget().setTextColor(mLabelColor)
            }
            if (mLabelSize != View.NO_ID) {
                getLabelWidget().setTextSize(TypedValue.COMPLEX_UNIT_PX, mLabelSize.toFloat())
            }
        } else {
            getLabelWidget().visibility = GONE
        }
        if (mShowContent) {
            getContentWidget().visibility = VISIBLE
            getContentWidget().text = mContentText
            if (mContentColor != View.NO_ID) {
                getContentWidget().setTextColor(mContentColor)
            }
            if (mContentSize != View.NO_ID) {
                getContentWidget().setTextSize(TypedValue.COMPLEX_UNIT_PX, mContentSize.toFloat())
            }
        } else {
            getContentWidget().visibility = View.GONE
        }
        if (mShowEdit) {
            getEditWidget().visibility = VISIBLE
            getEditWidget().setText(mEditText,TextView.BufferType.EDITABLE)
            getEditWidget().hint = mEditHideText
            if (mEditColor != View.NO_ID) {
                getEditWidget().setTextColor(mEditColor)
                getEditWidget().setHintTextColor(mEditColor)
            }
            if (mEditSize != View.NO_ID) {
                getEditWidget().setTextSize(TypedValue.COMPLEX_UNIT_PX, mEditSize.toFloat())
            }
        } else {
            getEditWidget().visibility = View.GONE
        }
        if (mShowRightImg) {
            getRightImage().visibility = View.VISIBLE
            if (mRightImgResId != View.NO_ID) {
                getRightImage().setImageResource(mRightImgResId)
            }
            if (mRightImgColor != View.NO_ID) {
                getRightImage().imageTintList = ColorStateList.valueOf(mRightImgColor)
            }
        } else {
            getRightImage().visibility = View.GONE
        }
        if (mShowLine) {
            getBottomLine().visibility = View.VISIBLE
            if (mLineColor != View.NO_ID) {
                getBottomLine().backgroundTintList = ColorStateList.valueOf(mLineColor)
            }
        }
    }

    fun getLabelWidget(): TextView = tv_label

    fun getContentWidget(): TextView = tv_content

    fun getEditWidget():EditText=tv_edit

    fun getRightImage(): ImageView = img_right

    fun getBottomLine(): View = line

    fun getContentText(): String = tv_content.text.toString().trim()

    fun getEditText():String = tv_edit.text.toString().trim()
}